﻿namespace WMS6.Web.Inventory
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.CommonMethodServiceRef;
    using WMS6.Web.InvMovementServiceRef;

    public class OrderMovement : BasePage
    {
        protected HtmlInputButton btnAction;
        protected Button btnAddDetail;
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnDeleteDetail;
        protected Button btnGoBack;
        protected Button btnImportData;
        protected Button btnNew;
        protected Button btnSave;
        protected DropDownList ddlMovementType;
        protected DropDownList ddlReasonCode;
        protected DropDownList ddlStatus;
        protected HtmlForm F5221;
        protected GridView gvMovementDetail;
        protected HtmlInputHidden hdncalendarFlag;
        protected HtmlInputHidden hdnCopy;
        protected HtmlInputHidden hdnDataFormat;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnLineID;
        protected HtmlInputHidden hdnMovementID;
        protected HtmlInputHidden hdnType;
        protected HtmlHead Head1;
        protected HtmlInputButton htmlbtnReport;
        protected HtmlImage imgOwnerID;
        protected Label lblApprovedBy;
        protected Label lblApprovedDate;
        protected Label lblCreatedBy;
        protected Label lblCreatedDate;
        protected Label lblMovementDate;
        protected Label lblMovementType;
        protected Label lblMovmentID;
        protected Label lblMsg;
        protected Label lblOrderMovement;
        protected Label lblOwnerID;
        protected Label lblReasonCode;
        protected Label lblRemark;
        protected Label lblStatus;
        protected Label lblUdf04;
        protected Label lblUdf09;
        protected Label lblUdf1;
        protected Label lblUdf10;
        protected Label lblUdf2;
        protected Label lblUdf3;
        protected Label lblUdf5;
        protected Label lblUdf6;
        protected Label lblUdf7;
        protected Label lblUdf8;
        protected LinkButton lkbAllocate;
        protected LinkButton lkbConfirm;
        protected LinkButton lkbRevertAllocate;
        protected LinkButton lnkbtnMovementOrder;
        protected LinkButton lnkbtnPodSave;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected Label one3;
        protected ScriptManager ScriptManager1;
        protected HtmlTableCell tdlkbAllocate;
        protected HtmlTableCell tdlkbConfirm;
        protected HtmlTableCell tdlkbRevertAllocate;
        protected HtmlTableCell tdlnkbtnMovementOrder;
        protected HtmlTableCell tdlnkbtnPodSave;
        protected TextBox txtApproveDate;
        protected TextBox txtApprovedBy;
        protected TextBox txtCreatedBy;
        protected TextBox txtCreatedDate;
        protected TextBox txtMovementDate;
        protected TextBox txtMovmentID;
        protected TextBox txtOwnerID;
        protected TextBox txtRemark;
        protected TextBox txtUdf1;
        protected TextBox txtUdf10;
        protected TextBox txtUdf2;
        protected TextBox txtUdf3;
        protected TextBox txtUdf4;
        protected TextBox txtUdf5;
        protected TextBox txtUdf6;
        protected TextBox txtUdf7;
        protected TextBox txtUdf8;
        protected TextBox txtUdf9;
        protected UpdatePanel up1;

        protected void btnAddDetail_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.ddlStatus.SelectedValue.ToUpper() == "NEW")
                {
                    if (string.IsNullOrEmpty(this.txtOwnerID.Text.Trim()))
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F4100_002");
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(this.hdnLineID.Value))
                        {
                            this.InsertMovementInfo();
                            this.hdnLineID.Value = "10";
                        }
                        base.Response.Redirect(string.Format("OrderMovementDetail.aspx?from={0}&ID={1}&LineID={2}&MovementType={3}&Owner={4}", new object[] { base.UrlEncode(this.hdnFrom.Value), base.UrlEncode(this.txtMovmentID.Text.Trim()), base.UrlEncode(this.hdnLineID.Value), base.UrlEncode(this.ddlMovementType.SelectedValue), base.UrlEncode(this.txtOwnerID.Text.Trim()) }), true);
                    }
                }
                else
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F4100_015");
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',3,3);</script>", false);
            }
        }

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtMovmentID.Text = "";
            this.hdnMovementID.Value = "";
            this.txtMovmentID.Enabled = true;
            this.ddlStatus.SelectedValue = "New";
            this.btnAddDetail.Visible = false;
            this.btnDeleteDetail.Visible = false;
            this.hdnFlag.Value = "0";
            this.hdnCopy.Value = "1";
            this.lblMsg.Text = base.GetGlobalMsgResource("F4100_014");
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txtMovmentID.Text.Trim()))
            {
                try
                {
                    if (this.ddlStatus.SelectedValue.ToUpper() != "NEW")
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F4100_011");
                    }
                    else
                    {
                        List<string> MovementIDs = new List<string> {
                            this.txtMovmentID.Text.Trim()
                        };
                        this.lblMsg.Text = InvMovementServiceClientProxy.GetInstance().DeleteMovement(MovementIDs) + base.GetGlobalMsgResource("F4100_012");
                        this.btnSave.Enabled = false;
                        this.btnDelete.Enabled = false;
                        this.btnCopy.Enabled = false;
                        base.DataEntityViewState = null;
                    }
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F4100_013") + ex.Message;
                }
            }
        }

        protected void btnDeleteDetail_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(this.txtMovmentID.Text.Trim()) && (this.hdnFlag.Value == "1"))
                {
                    List<string> lineID = base.GetSelectedIDsInGridView(this.gvMovementDetail, "LineID");
                    List<string> listStatus = base.GetSelectedIDsInGridView(this.gvMovementDetail, "Status");
                    if ((listStatus != null) && (listStatus.Count > 0))
                    {
                        for (int i = 0; i < listStatus.Count; i++)
                        {
                            if (listStatus[i].ToUpper() != "NEW")
                            {
                                this.lblMsg.Text = base.GetGlobalMsgResource("F4100_011");
                                return;
                            }
                        }
                    }
                    if ((lineID.Count > 0) && (lineID.Count > 0))
                    {
                        this.lblMsg.Text = InvMovementServiceClientProxy.GetInstance().DeleteMovementDetail(this.txtMovmentID.Text, lineID) + base.GetGlobalMsgResource("F4100_012");
                        this.SetMovementInfo2Form();
                    }
                    else
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F4100_017");
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F4100_019") + ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',3,3);</script>", false);
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("OrderMovementQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("OrderMovementList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnImportData_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.ddlStatus.SelectedValue.ToUpper() == "NEW")
                {
                    if (string.IsNullOrEmpty(this.txtOwnerID.Text.Trim()))
                    {
                        this.lblMsg.Text = base.GetGlobalMsgResource("F4100_002");
                    }
                    else
                    {
                        if (string.IsNullOrEmpty(this.hdnLineID.Value))
                        {
                            this.InsertMovementInfo();
                            this.hdnLineID.Value = "10";
                        }
                        ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',3,3);DataImport();</script>", false);
                    }
                }
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',3,3);</script>", false);
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtOwnerID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F4100_002");
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',1,3);</script>", false);
            }
            else
            {
                if (this.hdnFlag.Value == "1")
                {
                    this.UpdateMovementInfo();
                }
                else
                {
                    this.InsertMovementInfo();
                }
                if (this.hdnCopy.Value == "1")
                {
                    this.SetMovementInfo2Form();
                }
                this.btnAddDetail.Visible = true;
                this.btnDeleteDetail.Visible = true;
            }
        }

        protected string FormatMovementUrl(string url, string receiptID, string receiptLineID, string ownerID)
        {
            return string.Format(url, new object[] { this.hdnFrom.Value.Trim(), receiptID, receiptLineID, ownerID, this.ddlMovementType.SelectedValue });
        }

        protected void gvMovementDetail_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F5221", this.gvMovementDetail);
        }

        protected void gvMovementDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            (sender as GridView).PageIndex = e.NewPageIndex;
            (sender as GridView).DataSource = (base.DataEntityViewState as MovementInfo).MovementDetailInfos;
            (sender as GridView).DataBind();
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',6,6) </script>", false);
        }

        protected void gvMovementDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvMovementDetail.PageIndex * this.gvMovementDetail.PageSize;
                e.Row.Cells[1].Text = ((e.Row.RowIndex + 1) + i).ToString();
                if (e.Row.FindControl("lblSkuDescr") != null)
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                }
            }
        }

        protected void gvMovementDetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                string sortDirection = string.Empty;
                if (base.GridViewSortDirection == SortDirection.Ascending)
                {
                    base.GridViewSortDirection = SortDirection.Descending;
                    sortDirection = "DESC";
                }
                else
                {
                    base.GridViewSortDirection = SortDirection.Ascending;
                    sortDirection = "ASC";
                }
                MovementInfo movementInfo = base.DataEntityViewState as MovementInfo;
                DataView dv = CADataConverter.ToDataTable(movementInfo.MovementDetailInfos).DefaultView;
                dv.Sort = e.SortExpression + " " + sortDirection;
                base.BindData2GridView(this.gvMovementDetail, dv);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',3,3);</script>", false);
            }
        }

        private void InsertMovementInfo()
        {
            try
            {
                MovementInfo movementInfo = new MovementInfo();
                if (base.DataEntityViewState != null)
                {
                    movementInfo = base.DataEntityViewState as MovementInfo;
                }
                movementInfo = this.SetMovementInfo(movementInfo);
                movementInfo.CreatedBy = base.UserInfo.UserID;
                movementInfo.CreatedDate = DateTime.Now;
                movementInfo.ApprovedDate = DateTime.Now;
                string ovementID = InvMovementServiceClientProxy.GetInstance().InsertMovement(movementInfo);
                movementInfo.MovementID = ovementID;
                this.lblMsg.Text = ovementID + base.GetGlobalMsgResource("F4100_009");
                this.txtMovmentID.Text = ovementID;
                base.DataEntityViewState = movementInfo;
                this.hdnFlag.Value = "1";
                this.hdnMovementID.Value = ovementID;
                this.txtMovmentID.Enabled = false;
                if ((movementInfo.MovementDetailInfos != null) && (movementInfo.MovementDetailInfos.Count > 0))
                {
                    this.hdnLineID.Value = (movementInfo.MovementDetailInfos[movementInfo.MovementDetailInfos.Count - 1].LineID + 10).ToString();
                }
                else
                {
                    this.hdnLineID.Value = "10";
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F4100_010") + ex.Message;
            }
        }

        protected void lkbAllocate_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.ddlStatus.SelectedValue.ToUpper() != "NEW")
                {
                    this.lblMsg.Text = "单据状态不为新建,无法进行分配操作!";
                }
                else
                {
                    string result = InvMovementServiceClientProxy.GetInstance().AllocateMovement(this.txtMovmentID.Text.Trim());
                    if (!string.IsNullOrEmpty(result))
                    {
                        this.lblMsg.Text = result + " 分配成功!";
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = "分配失败:" + ex.Message;
            }
            finally
            {
                this.SetMovementInfo2Form();
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>$.unblockUI();</script>", false);
            }
        }

        protected void lkbConfirm_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.ddlStatus.SelectedValue.ToUpper() != "ALLOCATED")
                {
                    this.lblMsg.Text = "单据状态不为分配,无法进行确认操作!";
                }
                else if (!string.IsNullOrEmpty(InvMovementServiceClientProxy.GetInstance().MovementConfirm(this.txtMovmentID.Text.Trim())))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F5200_004");
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = "操作失败：" + ex.Message;
            }
            finally
            {
                this.SetMovementInfo2Form();
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>$.unblockUI();</script>", false);
            }
        }

        protected void lkbRevertAllocate_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.ddlStatus.SelectedValue.ToUpper() != "ALLOCATED")
                {
                    this.lblMsg.Text = "单据状态不为分配,无法进行取消分配操作!";
                }
                else
                {
                    string result = InvMovementServiceClientProxy.GetInstance().RevertAllocateMovement(this.txtMovmentID.Text.Trim());
                    if (!string.IsNullOrEmpty(result))
                    {
                        this.lblMsg.Text = result + " 取消分配成功!";
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = "取消分配失败:" + ex.Message;
            }
            finally
            {
                this.SetMovementInfo2Form();
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>$.unblockUI();</script>", false);
            }
        }

        protected void lnkbtnPodSave_Click(object sender, EventArgs e)
        {
            try
            {
                UDFInfo udfInfo = new UDFInfo {
                    Udf1 = this.txtUdf1.Text,
                    Udf2 = this.txtUdf2.Text,
                    Udf3 = this.txtUdf3.Text,
                    Udf4 = this.txtUdf4.Text,
                    Udf5 = this.txtUdf5.Text,
                    Udf6 = this.txtUdf6.Text,
                    Udf7 = this.txtUdf7.Text,
                    Udf8 = this.txtUdf8.Text,
                    Udf9 = this.txtUdf9.Text,
                    Udf10 = this.txtUdf10.Text,
                    Remark = this.txtRemark.Text
                };
                if (!string.IsNullOrEmpty(CommoneMethodServiceClientProxy.GetInstance().UpdateUDF(this.txtMovmentID.Text.Trim(), udfInfo, "MOVEMENT")))
                {
                    this.lblMsg.Text = "自定义字段更新成功！";
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = "操作失败：" + ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>$.unblockUI();</script>", false);
            }
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlMovementType, "MoveType");
            base.BindData2DDLBySysCode(this.ddlStatus, "MovementStatus");
            base.BindData2DDLBySysCode(this.ddlReasonCode, "MoveReason");
            this.txtCreatedBy.Text = base.UserInfo.UserID;
            this.txtCreatedDate.Text = base.GetPreferenceShortDate(DateTime.Now);
            this.txtMovementDate.Text = base.GetPreferenceShortDate(DateTime.Now);
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnMovementID.Value = base.Request.QueryString["ID"];
                this.SetMovementInfo2Form();
                this.hdnFlag.Value = "1";
            }
            else
            {
                base.SetDefaultOwner(this.txtOwnerID);
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["detail"]))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',3,3);</script>", false);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private MovementInfo SetMovementInfo(MovementInfo receipt)
        {
            MovementInfo movementInfo = receipt;
            movementInfo.MovementID = this.txtMovmentID.Text.Trim();
            movementInfo.Status = this.ddlStatus.SelectedValue;
            movementInfo.OwnerID = this.txtOwnerID.Text.Trim();
            if (!string.IsNullOrEmpty(this.txtMovementDate.Text))
            {
                movementInfo.MovementDate = DateTime.Parse(this.txtMovementDate.Text.Trim());
            }
            else
            {
                movementInfo.MovementDate = DateTime.Now;
            }
            movementInfo.MovementType = this.ddlMovementType.SelectedValue;
            movementInfo.ReasonCode = this.ddlReasonCode.SelectedValue;
            movementInfo.Udf1 = this.txtUdf1.Text.Trim();
            movementInfo.Udf2 = this.txtUdf2.Text.Trim();
            movementInfo.Udf3 = this.txtUdf3.Text.Trim();
            movementInfo.Udf4 = this.txtUdf4.Text.Trim();
            movementInfo.Udf5 = this.txtUdf5.Text.Trim();
            movementInfo.Udf6 = this.txtUdf6.Text.Trim();
            movementInfo.Udf7 = this.txtUdf7.Text.Trim();
            movementInfo.Udf8 = this.txtUdf8.Text.Trim();
            movementInfo.Udf9 = this.txtUdf9.Text.Trim();
            movementInfo.Udf10 = this.txtUdf10.Text.Trim();
            movementInfo.Remark = this.txtRemark.Text.Trim();
            movementInfo.CreatedBy = this.txtCreatedBy.Text;
            movementInfo.ApprovedBy = this.txtApprovedBy.Text;
            movementInfo.IsActive = "Y";
            return movementInfo;
        }

        private void SetMovementInfo2Form()
        {
            try
            {
                string MovementID = this.hdnMovementID.Value.ToString();
                MovementInfo movementInfo = InvMovementServiceClientProxy.GetInstance().GetMovementInfoByID(MovementID);
                base.DataEntityViewState = movementInfo;
                if (movementInfo != null)
                {
                    this.txtMovmentID.Text = movementInfo.MovementID;
                    this.ddlStatus.SelectedValue = movementInfo.Status;
                    this.txtMovementDate.Text = base.GetPreferenceShortDate(movementInfo.MovementDate);
                    this.txtOwnerID.Text = movementInfo.OwnerID;
                    this.txtOwnerID.Enabled = false;
                    this.imgOwnerID.Visible = false;
                    this.ddlMovementType.SelectedValue = movementInfo.MovementType;
                    this.ddlMovementType.Enabled = false;
                    this.txtUdf1.Text = movementInfo.Udf1;
                    this.txtUdf2.Text = movementInfo.Udf2;
                    this.txtUdf3.Text = movementInfo.Udf3;
                    this.txtUdf4.Text = movementInfo.Udf4;
                    this.txtUdf5.Text = movementInfo.Udf5;
                    this.txtUdf6.Text = movementInfo.Udf6;
                    this.txtUdf7.Text = movementInfo.Udf7;
                    this.txtUdf8.Text = movementInfo.Udf8;
                    this.txtUdf9.Text = movementInfo.Udf9;
                    this.txtUdf10.Text = movementInfo.Udf10;
                    this.txtRemark.Text = movementInfo.Remark;
                    this.txtApprovedBy.Text = movementInfo.ApprovedBy;
                    this.txtApproveDate.Text = base.GetPreferenceShortDate(movementInfo.ApprovedDate);
                    if ((movementInfo.MovementDetailInfos != null) && (movementInfo.MovementDetailInfos.Count > 0))
                    {
                        movementInfo.MovementDetailInfos.Sort((Comparison<MovementDetailInfo>) ((m1, m2) => Comparer<int>.Default.Compare(m1.LineID, m2.LineID)));
                        this.hdnLineID.Value = (movementInfo.MovementDetailInfos[movementInfo.MovementDetailInfos.Count - 1].LineID + 10).ToString();
                    }
                    else
                    {
                        this.hdnLineID.Value = "10";
                    }
                    base.BindData2GridView(this.gvMovementDetail, movementInfo.MovementDetailInfos);
                    this.txtMovmentID.Enabled = false;
                    this.txtCreatedBy.Text = movementInfo.CreatedBy;
                    this.txtCreatedDate.Text = base.GetPreferenceShortDate(movementInfo.CreatedDate);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateMovementInfo()
        {
            try
            {
                if (this.ddlStatus.SelectedValue.ToUpper() != "NEW")
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("F4100_006");
                }
                else
                {
                    MovementInfo movementInfo = base.DataEntityViewState as MovementInfo;
                    movementInfo = this.SetMovementInfo(movementInfo);
                    movementInfo.UpdatedBy = base.UserInfo.UserID;
                    this.lblMsg.Text = InvMovementServiceClientProxy.GetInstance().UpdateMovement(movementInfo) + base.GetGlobalMsgResource("F4100_007");
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F4100_008") + ex.Message;
            }
        }
    }
}

